// const express = require('express')
// const app = express()
const xlsx = require('node-xlsx')
// const router = express.Router()
const http = require('http')

const fs = require('fs')
const path = require('path')
let result = []
const fileName = 'zxwms'
let TimeFilter = '2017-9'

fs.readFile(path.join(__dirname, 'log', fileName + '.txt'), function (err, data) {
  if (err) {
    console.log('读取日志文件失败!')
  } else {
    let p = /Author: (\w+) <(\w[-\w.+]*@[A-Za-z0-9][-A-Za-z0-9]+\.+[A-Za-z]{2,14})> Date:   ([\s\S]{29,30})      ([\w \！\!\-\.\,\，\。\‘\“\"\'\、\n/\·A-Z\u4e00-\u9fa5]+)  commit/g
    data = data.toString().replace(/\n/g, ' ')

    let res = []

    while (step = p.exec(data.toString())) {
      res.push({
        Author: step[1],
        Email: step[2],
        Time: step[3],
        Content: step[4],
      })
    }

    let i = 0
    for (let item of res) {
      i++
      let {Author, Time, Content} = item
      if (!result[Author]) result[Author] = [['Time', 'Content']]
      result[Author].push([new Date(Time).toLocaleString(), Content])
      // console.log(`${Author}\t${Email}\t${new Date(Time).toLocaleString()}\t${Content}`)
    }
    // for(item of result){
    //   let {Author,Email,Time,Content}=item;
    //   if(Author==='imzhangxing')console.log(`${Author}\t${Email}\t${new Date(Time).toLocaleString()}\t${Content}`);
    // }

    console.log('成功读取 共' + i + '条！')
    // console.log(data.toString())
    let option = []
    for (let index in result) {
      if (result.hasOwnProperty(index)) {
        console.log(index + ' 共' + (result[index].length - 1) + '条！')
        if (result[index].length > 0 && TimeFilter !== '') {
          let step = [['绩效完成内容']];
          let TimeFilterList = [];
          step[1] = [` ${TimeFilter}-01 到 ${TimeFilter}-07 `, ''];
          TimeFilterList[1] = new RegExp(`(^${TimeFilter}-[1-7] )`);
          step[2] = [` ${TimeFilter}-08 到 ${TimeFilter}-15 `, ''];
          TimeFilterList[2] = new RegExp(`(^${TimeFilter}-0[8-9])|(^${TimeFilter}-1[0-5])`);
          step[3] = [` ${TimeFilter}-16 到 ${TimeFilter}-23 `, ''];
          TimeFilterList[3] = new RegExp(`(^${TimeFilter}-1[6-9])|(^${TimeFilter}-2[0-3])`);
          step[4] = [` ${TimeFilter}-24 到 ${TimeFilter}-31 `, ''];
          TimeFilterList[4] = new RegExp(`(^${TimeFilter}-2[4-9])|(^${TimeFilter}-3[0-1])`);
          result[index].forEach(item => {
            for (let i = 1; i <= 4; i++) {
              if (TimeFilterList[i].test(item[0])) {
                step[i][1] += `${item[0]} ${item[1]}\n`;
              }
            }
          });
          option.push({
            name: index,
            data: step
          })
        } else {
          option.push({
            name: index,
            data: result[index]
          })
        }
      }
    }
    let buffer = xlsx.build(option)
    let outputPath = path.join(__dirname, 'output', fileName + '.xlsx');
    console.log(outputPath);
    fs.writeFileSync(outputPath, buffer, 'binary');
  }

})

// router.get('/exportExcel', function (req, res, next) {
// // write
//   let option = []
//   for (index in result) {
//     if (result.hasOwnProperty(index)) {
//       option.push({
//         name: index,
//         data: result[index]
//       })
//       console.log(index + ' 共' + result[index].length + '条！')
//     }
//   }
//   let buffer = xlsx.build(option)
//   fs.writeFileSync('b.xlsx', buffer, 'binary')
//   res.send('export successfully!')
//
// })
//
// app.use('/', router)
// app.set('port', 19999)
// const server = http.createServer(app)
// server.listen(19999)
